import { defineStore } from 'pinia'

import { getListApi, getdeleteApi, postSaveApi, postUpdateApi } from '@/api/menuManagement'

const orderMap: any = {
    ascend: 1,
    descend: -1
}

export const useManagementStore = defineStore('ManagementStore', {
    state: () => {
        return {
            list: [],
            classify: [],
            total: 0,//数据总共多少条
            page: 1,
            pageSize: 3,
            //查询
            query: '',
            query1: '',
            //排序
            by: undefined,
            order: 'ascend'
        }
    },
    getters: {

    },
    actions: {
        async getList() {
            if (!this.order) {
                const { data, total, datas } = await getListApi({
                    page: this.page - 1,
                    pageSize: this.pageSize,
                    query: this.query,
                    query1: this.query1
                })
                this.list = data
                this.total = total
                this.classify = datas
            } else {
                const { data, total, datas } = await getListApi({
                    page: this.page - 1,
                    pageSize: this.pageSize,
                    query: this.query,
                    query1: this.query1,
                    by: this.by,
                    order: orderMap[this.order]
                })
                this.list = data
                this.total = total
                this.classify = datas
            }
        },
        async deleteId(_id: string) {
            await getdeleteApi({ _id })
            this.getList()
        },
        async newly(from: any) {
            if (from._id) {
                await postUpdateApi(from)
            } else {
                await postSaveApi(from)
            }
        }
    }
})